﻿//TODO: test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is used to show
    /// </summary>
    public class FindColors : BaseImageFilter
    {
        protected bool m_bFindOnly = true;
        protected IEnumerable<Color> m_aColor = null;
        protected Color m_colorToReplaceWith = Color.Transparent;

        /// <summary>
        /// Default constructor
        /// </summary>
        public FindColors()
            : this(new List<Color>()
            {
                Color.Red, 
                Color.Green, 
                Color.Yellow, 
                Color.Turquoise, 
                Color.Purple, 
                Color.Blue, 
                Color.Black, 
                Color.White}, 
            Color.Black)
        {
        }

        /// <summary>
        /// primary constructor
        /// </summary>
        /// <param name="_aColor"></param>
        /// <param name="_colorToReplaceWith"></param>
        public FindColors(IEnumerable<Color> _aColor, Color _colorToReplaceWith)
            : this(true, _aColor, _colorToReplaceWith)
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="_bFindOnly"></param>
        /// <param name="_aColor"></param>
        /// <param name="_colorToReplaceWith"></param>
        public FindColors(bool _bFindOnly, IEnumerable<Color> _aColor, Color _colorToReplaceWith )
        {
            m_bFindOnly = _bFindOnly;
            m_aColor = _aColor;
            m_colorToReplaceWith = _colorToReplaceWith;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_findColors"></param>
        internal FindColors(FindColors _findColors)
        {
            this.m_aColor = _findColors.m_aColor;
            this.m_colorToReplaceWith = _findColors.m_colorToReplaceWith;
            this.m_bFindOnly = _findColors.m_bFindOnly;
        }

        /// <summary>
        /// Gets or sets find only
        /// </summary>
        public bool FindOnly
        {
            get { return m_bFindOnly; }
            set { m_bFindOnly = value; }
        }

        /// <summary>
        /// Gets or sets the color list
        /// </summary>
        public IEnumerable<Color> ColorList
        {
            get { return m_aColor; }
            set { m_aColor = value; }
        }

        /// <summary>
        /// Gets or sets the color to replace with
        /// </summary>
        public Color ColorToReplaceWith
        {
            get { return m_colorToReplaceWith; }
            set { m_colorToReplaceWith = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            if (m_bFindOnly)
            {
                FindOnlyInternal(_bitmap, m_aColor, m_colorToReplaceWith);
            }
            else
            {
                FindNoneOfInternal(_bitmap, m_aColor, m_colorToReplaceWith);
            }
            return _bitmap;
        }

        /// <summary>
        /// Finds only given colors, all pixels which are not of a found color are eliminated (replaced by colorToReplaceWith)
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <param name="aColor"></param>
        /// <param name="colorToReplaceWith"></param>
        private static void FindOnlyInternal(UnsafeBitmap _bitmap, IEnumerable<Color> aColor, Color colorToReplaceWith)
        {
            UnsafeBitmap bitmap = _bitmap;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            Color color = Color.Transparent;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    if (!aColor.Contains(color))
                    {
                        bitmap.SetPixel(x, y, colorToReplaceWith);
                    }
                }
            }
            bitmap.Dispose();
        }

        /// <summary>
        /// Fond none of internal
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <param name="aColor"></param>
        /// <param name="colorToReplaceWith"></param>
        private static void FindNoneOfInternal(UnsafeBitmap _bitmap, IEnumerable<Color> aColor, Color colorToReplaceWith)
        {
            UnsafeBitmap bitmap = _bitmap;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            Color color = Color.Transparent;
            
            int x = 0;
            int y = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    if (aColor.Contains(color))
                    {
                        bitmap.SetPixel(x, y, colorToReplaceWith);
                    }
                }
            }
            bitmap.Dispose();
        }

        /// <summary>
        /// Use this to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new FindColors(this);
        }
    }
}
